var equipment_type=1;
var is_bool={is_bool_1:true,is_bool_2:true,is_bool_3:true};
var is_change={is_change_1:true,is_change_2:true,is_change_3:true};
var page_no={page_no_1:1,page_no_2:1,page_no_3:1};
var param='';
var pic_label='';

$(function(){
    equipment_type=$('.tabList-active').attr('data-type');
	$('.tabListUl li').click(function(){
        if(is_change['is_change_'+equipment_type]){
            $('.tabListUl li').removeClass('tabList-active');
            $(this).addClass('tabList-active');
            $('.tab').removeClass('tab-active');
            $('.tab').eq($(this).index()).addClass('tab-active');
            equipment_type=$(this).attr('data-type');
        }
    });

    $('.tabListUl li').each(function(){
        if($(this).hasClass('default-list')){
            getEquipmentList($(this).attr('data-type'));
        }
        else{
            is_bool['is_bool_'+$(this).attr('data-type')]=false;
        }
    });
    

    //滚动到底
    $(window).scroll(function(){
    　　var scrollTop = $(this).scrollTop();
    　　var scrollHeight = $(document).height();
    　　var windowHeight = $(this).height();
    　　if(scrollTop + windowHeight == scrollHeight&&is_bool['is_bool_'+equipment_type]){
            // alert(equipment_type);
            getEquipmentList(equipment_type);
    　　}
    });

    doDefault();

    //搜索xx
    $('.icon-remove-code').click(function(){
        param='';
        is_bool={is_bool_1:true,is_bool_2:true,is_bool_3:true};
        is_change={is_change_1:true,is_change_2:true,is_change_3:true};
        page_no={page_no_1:1,page_no_2:1,page_no_3:1};
        $('.tab ul').html('');
        $(this).prev().val('');
        
        $('.tabListUl li').each(function(){
            getEquipmentList($(this).attr('data-type'));
        });

    });
    //搜索
    $('.search-button button').click(function(){
        param=$('.param').val();
        is_bool={is_bool_1:true,is_bool_2:true,is_bool_3:true};
        is_change={is_change_1:true,is_change_2:true,is_change_3:true};
        page_no={page_no_1:1,page_no_2:1,page_no_3:1};
        $('.tab ul').html('');

        $('.tabListUl li').each(function(){
            getEquipmentList($(this).attr('data-type'));
        });
    });

    $('.back-to-pic-document i').click(function(){
        if(is_change['is_change_'+$(this).attr('data-type')]){
            $('.pic-box').hide();
            $('.document-box').show();
            is_bool['is_bool_'+$(this).attr('data-type')]=false;
        }
    });
})


function getEquipmentList(func_equipment_type,label) {
    var label=label||false;
    //禁止滚动,禁止切换
    is_bool['is_bool_'+func_equipment_type]=false;
    is_change['is_change_'+func_equipment_type]=false;
    $('.tab').eq(Number(func_equipment_type)-1).find('.no-data').hide();

    $('.load').show();
    var datas={
            park_id: $('#park_id').val(),
            equipment_type:func_equipment_type,
            page_no:page_no['page_no_'+func_equipment_type],
            param:param
        };
    if(label){
        datas.label=label;
    }
    $.ajax({
        type: "post",
        url: "Api/Equipment/getEquipmentList",
        data:datas ,
        async: true,
        dataType: 'json',
        success: function(data) {
            setTimeout(function(){
                is_change['is_change_'+func_equipment_type]=true;
                $('.load').hide();
                if (data.status) {
                    var equipment_type_demo='.equipment-type-'+func_equipment_type+'-demo';
                    // alert($(equipment_type_demo).html());
                    var t=JSON.parse(data.v);
                    $.each(t.data,function(k,v){
                        $(equipment_type_demo).find('.equipment-img').attr('src',v.img);
                        $(equipment_type_demo).find('li').attr('data-link',v.link).attr('data-id',v.id);
                        $(equipment_type_demo).find('.equipment-name').html(v.name);
                        $(equipment_type_demo).find('.equipment-size').html(v.size);
                        $(equipment_type_demo).find('.equipment-introduction').html(v.introduction);
                        $('.tab').eq(Number(func_equipment_type)-1).find('ul').append($(equipment_type_demo).html());
                    });
                    is_bool['is_bool_'+func_equipment_type]=t.have;
                    page_no['page_no_'+func_equipment_type]=Number(page_no['page_no_'+func_equipment_type])+1;
                    if(!t.have){
                        $('.tab').eq(Number(func_equipment_type)-1).find('.no-data').show();
                    }
                    console.log(is_bool);
                    console.log(page_no);
                    setIntroduction();
                } else {

                }
            },1000);
        }
    });
}

function showEquipment1(obj){
    if($(obj).attr('data-link')!=''){
        location.href=$(obj).attr('data-link');
    }
}

function showEquipment2(obj){
    if($(obj).attr('data-link')!=''){
        showImages($(obj).attr('data-link'));
    }
}

function showEquipment3(obj){
    if($(obj).attr('data-link')!=''){
        location.href=getRootPath()+'/GasHome/Equipment/equipmentContent?park_id='+$('#park_id').val()+'&equipment_id='+$(obj).attr('data-id');
    }
} 


//获取微信jssdk凭证
function doDefault(successCallBack,errorCallBack) {
    var successCallBack=successCallBack||false; 
    var errorCallBack=errorCallBack||false; 
    $.ajax({
        type: "post",
        url: "CommonApi/CommonWx/getWxJsSdkConfig",
        data: {
            url: window.location.href,
            park_id:$('#park_id').val()
        },
        async: true,
        dataType: 'json',
        success: function(data) {
            if (data.status) {
                var t = JSON.parse(data.v);
                wx.config({
                    beta: true,
                    debug: false,
                    appId: t.appId,
                    timestamp: t.timestamp,
                    nonceStr: t.nonceStr,
                    signature: t.signature,
                    jsApiList: ['checkJsApi',"previewImage"] // 必填，需要使用的JS接口列表
                });
                if(successCallBack){
                    successCallBack();
                }
            } else {
                errorAlert(data.msg);
                if(errorCallBack){
                    errorCallBack();
                }
            }
        }
    });
}

var showImages=function(data_link){
    var data_link_array=data_link.split('&&');
    var current=data_link_array[0];
    // alert(document.domain);
    if(current.substr(0,7).toLowerCase() == "http://"||current.substr(0,8).toLowerCase() == "https://"){
        var current=current;
    }
    else{
        if(current.substr(0,8).toLowerCase()=='/ueditor'){
            var current='http://'+document.domain+current;
        }
        else if(current.substr(0,1).toLowerCase()=='/'){
            var current=getRootPath()+current;
        }
        else{
            var current=getRootPath()+'/'+current;
        }  
    }
    wx.ready(function() {
        wx.checkJsApi({
            jsApiList: ['previewImage'], // 需要检测的JS接口列表，所有JS接口列表见附录2,  
            success: function(res) {
                if (res.checkResult.onMenuShareAppMessage == false) {
                    alert("您的微信版本太低！");
                    return;
                }
            }
        });
        var img=[];
        for (var i = 0; i < data_link_array.length; i++) {
            if(data_link_array[i].substr(0,7).toLowerCase() == "http://"||data_link_array[i].substr(0,8).toLowerCase() == "https://"){
                img.push(data_link_array[i]);
            }
            else{
                if(data_link_array[i].substr(0,8).toLowerCase()=='/ueditor'){
                    img.push('http://'+document.domain+data_link_array[i]);
                }
                else if(data_link_array[i].substr(0,1).toLowerCase()=='/'){
                    img.push(getRootPath()+data_link_array[i]);
                }
                else{
                    img.push(getRootPath()+'/'+data_link_array[i]);
                }    
            } 
        } 
        wx.previewImage({
            current: current, // 当前显示图片的http链接
            urls: img // 需要预览的图片http链接列表
        });
    });
}


//装备简介动态高度
function setIntroduction(){
    // $('.equipment-introduction').each(function(){
    //     $(this).height(Number($(this).parents('.tabBox').find('.equipment-img').height())-35);
    // });
}


function showPic(obj){
    pic_label=$(obj).attr('data-label');
    func_equipment_type=$(obj).attr('data-type');
    //页码重置
    page_no['page_no_'+func_equipment_type]=1;

    $('.document-box').hide();
    $('.pic-box').show();
    $('.pic-box ul').html('');
    getEquipmentList(func_equipment_type,pic_label);
}